/**
 * Created by zhoulin on 2019/4/3.
 */
var companyId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.companyId);
var schoolId = sessionStorageUtils.getJson(storageKey.sessionStorageKey.schoolId);
var schoolMap = sessionStorageUtils.getJson(storageKey.sessionStorageKey.schoolMap);

var type = "";
//表格初始化配置
var tableInit = function () {
    var columns = [
        {
            title: '全选',
            field: 'select',
            //复选框
            checkbox: true,
            width: 25,
            align: 'center',
            valign: 'middle'
        },
        {
            field: 'id',
            title: 'ID',
            align: 'center',
            valign: 'middle',
        },
        {
            field: 'nickname',
            title: '领券人',
            align: 'center',
            valign: 'middle',
            // visible: false,
        },
        {
            field: 'name',
            title: '券名称',
            align: 'center',
            valign: 'middle',
        },
        {
            field: 'type',
            title: '优惠券类型',
            align: 'center',
            valign: 'middle',
            visible: false,
            formatter: function (value) {
                if (!empty(value)) {
                    value = typeMap.get(value);
                    if (empty(value)) {
                        return "暂无";
                    }
                    return '<span class="label label-success">' + value + '</span>';
                } else {
                    return "暂无";
                }
            }
        },
        {
            field: 'coupon_money',
            title: '优惠券面值',
            align: 'center',
            valign: 'middle',
            // visible: false,
            formatter: function (value, row, index) {
                if (!empty(row.type)) {
                    var val = typeMap.get(row.type);
                    if (empty(val)) {
                        return value;
                    }
                    if (val.indexOf("折扣") != -1) {
                        return value + " 折";
                    } else {
                        return value + " 元";
                    }
                } else {
                    return value;
                }
            }
        },
        {
            field: 'min_use_money',
            title: '最低消费金额',
            align: 'center',
            valign: 'middle',
            visible: false,
        },
        {
            field: 'use_term',
            title: '使用期限天数',
            align: 'center',
            valign: 'middle',
            visible: false,
        },

        {
            field: 'begin_time',
            title: '有效时间',
            align: 'center',
            valign: 'middle',
            formatter: function (value, row, index) {
                if (!empty(value)) {
                    return formatDateTime(row.begin_time) + "~" + formatDateTime(row.end_time);
                } else {
                    return "-"
                }
            }
        },

        {
            field: 'pstatus',
            title: '是否有效',
            align: 'center',
            valign: 'middle',
            visible: false,
            formatter: function (value, row, index) {
                if (value == 0) {
                    return "失效"
                } else if (value == 1) {
                    return "正常"
                }
                return "未开启";
            }
        },

        {
            field: 'get_way',
            title: '获取方式',
            align: 'center',
            valign: 'middle',
            // visible: false,
            formatter: function (value) {
                if (!empty(value)) {
                    value = getWayMap.get(value);
                    if (empty(value)) {
                        return "暂无";
                    }
                    return '<span class="label label-success">' + value + '</span>';
                } else {
                    return "暂无";
                }
            }
        },
        {
            field: 'status',
            title: '券状态',
            align: 'center',
            valign: 'middle',
            // visible: false,
            formatter: function (value, row, index) {
                if (value == -1) {
                    return "已过期"
                } else if (value == 0) {
                    return "未使用"
                } else if (value == 1) {
                    return "已使用"
                }
                return "-";
            }
        },
        {
            field: 'gmt_create',
            title: '创建时间',
            align: 'center',
            visible: false,
            formatter: function (value, row, index) {
                return formatDateTime(value);
            }
        },
    ];

    //请求服务数据时所传参数
    function queryParams(params) {
        return {
            pageNumber: params.pageNumber,
            pageSize: params.pageSize,
            compayId: companyId,
            appId: null,
            schoolId: schoolId,
            searchText: params.searchText,

            couponPushId: aGET.couponPushId,
            type: $("#type").val(),
            status: $("#status").val(),
            getWay: $("#getWay").val(),
            isFullCourt: $("#isFullCourt").val(),
            useScene: $("#useScene").val(),
            isOpenExchange: $("#isOpenExchange").val(),
            beginTime: times != '' ? (times.split(' ~ ')[0] + ":00") : '',
            endTime: times != '' ? (times.split(' ~ ')[1] + ":00") : '',
        }
    }

    var tableConfig = $.extend(bootstrapTableConfig(_HOST + _API.marketing_couponPush_getCouponPushUserLog, queryParams, columns), {
        //这里是其他的一些个性化的 bootstrap-table options
        showColumns: true,//列显示操作按钮
        ajaxOptions: { //设置自定义请求头
            headers: {
                'Authorization': sessionStorageUtils.getJson(storageKey.sessionStorageKey.token),
                'permCoding': 'couponPush:getCouponPushUserLog'
            }
        },
        //例如格式化数据
        responseHandler: function (res) {
            //在ajax获取到数据，渲染表格之前，修改数据源
            //console.log(res)
            if (res.success) {
                //更新本地token
                sessionStorageUtils.setJsonStr(storageKey.sessionStorageKey.token, res.token);
            }
            return res;
        }
    });
    //调用实例化
    $('#bootstrapTableEvents').bootstrapTable(tableConfig);
    $(".search input").attr('placeholder', '输入名称搜索');
};

var app = new Vue({
    el: '#app',
    data: {
        permissions: ['couponPush:getCouponPushUserLog', 'couponPush:batchStatus', 'couponPush:del', 'couponPush:list'],//本页权限数组
        selects: [],
        useSceneList: [],
        waysList: [],
    },
    //进入页面初始化方法
    created: function () {
        var that = this;
        var token = sessionStorageUtils.getJson(storageKey.sessionStorageKey.token);
        if (empty(token)) {
            layer.msg("<span style='color:red'>登录超时！请重新登录！</span>", {time: 1500, icon: 5});
            setTimeout(function () {
                window.location.href = _LOGIN_PAGE;
            }, 1200);

            return;
        }
    },
    methods: {
        //权限控制
        permission: function (index) {
            return (_PERMISSIONS.indexOf(this.permissions[index]) != -1);
        },
        // 得到字典map
        getDicList: function (keyTable) {
            var that = this;
            var ret = new Map();
            if (keyTable == 'couponType') {
                that.selects = [{keyS: "", keyValue: "优惠卷分类"}];
            } else if (keyTable == 'useScene') {
                that.useSceneList = [{keyS: "", keyValue: "使用场景"}];
            } else if (keyTable == 'couponGetWay') {
                that.waysList = [{keyS: "", keyValue: "优惠券获取方式"}];
            }
            Get(that, _HOST + _API.common_dic_dicList + "?keyTable=" + keyTable)
                .then(function (response) {
                    // 响应成功回调
                    console.log(response);
                    if (response) {
                        if (response.success) {
                            var list = response.list;
                            for (var i = 0; i < list.length; i++) {
                                if (keyTable == 'useScene') {
                                    list[i].keyValue = list[i].keyValue + "场景";
                                }
                                ret.set(parseInt(list[i].keyS), list[i].keyValue);
                            }
                            if (keyTable == 'couponType') {
                                that.selects = that.selects.concat(response.list);
                            } else if (keyTable == 'useScene') {
                                that.useSceneList = that.useSceneList.concat(list);
                            } else if (keyTable == 'couponGetWay') {
                                that.waysList = that.waysList.concat(list);
                            }
                        } else {
                            layer.msg(response.msg, {time: 1500, icon: 5});
                        }
                    }
                });
            return ret;
        },
        selectStatus: function () {
            $('#bootstrapTableEvents').bootstrapTable('refresh');
        },
    },
});

var typeMap = app.getDicList("couponType");
var sceneMap = app.getDicList("useScene");
var getWayMap = app.getDicList("couponGetWay");
var times = '';
// 时间选择
laydate.render({
    elem: '#time'
    , type: 'datetime'
    , range: '~' //或 range: '~' 来自定义分割字符
    , format: 'yyyy-MM-dd HH:mm'
    , done: function (value, date, endDate) {
        times = value;
        $('#bootstrapTableEvents').bootstrapTable('refresh');
    }
});

function update(index) {
    app.update(index);
}

$(function () {
    //初始化表格
    tableInit();
});
